package com.fz.security.service;


import com.fz.entity.system.User;
import com.fz.security.vo.JwtUser;
import com.fz.service.system.UserService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Service("userDetailsService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class UserDetailsServiceImpl implements UserDetailsService {

    private final UserService userService;


    public UserDetailsServiceImpl(UserService userService) {
        this.userService = userService;
    }


    @Override
    public UserDetails loadUserByUsername(String username) {
        User user = userService.findUserByLoginid(username);
        return createJwtUser(user);
    }



    public UserDetails createJwtUser(User user){
        return new JwtUser(user.getId().toString(),user.getUsername(),user.getLoginId(),user.getPassword(), userService.mapToRoleAuthoritiesMenu(String.valueOf(user.getId()))
        );
    }

}
